package com.acc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.acc.dao.AuthorityDAO;
import com.acc.entity.AdminRole;
import com.acc.utils.JDBCTools;
/**
 * 
 * @author 朱春阳
 *
 */
public class AuthorityDAOImpl implements AuthorityDAO{
	
	/**
	 * 根据条件获取对应的管理员---角色关系表
	 */
	@Override
	public List<AdminRole> getAllAdminRole(String sql, Object...objects) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<AdminRole> adminroles = new ArrayList<>();
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {
				
				pst.setObject(i, object);
				i++;
			}
			
			rs = pst.executeQuery();
			AdminRole adminrole = null;
			while(rs.next()) {
				adminrole = new AdminRole();
				adminrole.setId(rs.getInt("id"));
				adminrole.setAdminName(rs.getString("admin_name"));
				adminrole.setAdminRole(rs.getString("admin_role"));
				adminroles.add(adminrole);
			}
			
			return adminroles;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCTools.close(con, pst, rs);
		}
		
		return null;
	}

	/**
	 * 删除ADMIN-ROLE关系表中的一条记录数
	 */
	@Override
	public Integer deleteOneAdminRole(String sql, Object... objects) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {
				
				pst.setObject(i, object);
				i++;
			}
			
			Integer num = pst.executeUpdate();
			return num;
		}catch(Exception e){
			e.printStackTrace();	
		}finally {
			JDBCTools.close(con, pst, null);
		}
		return null;
	}

	/**
	 * 往ADMIN-ROLE关系表中添加一条记录
	 */
	@Override
	public Integer addOneAdminRole(String sql, Object...objects) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {
				
				pst.setObject(i, object);
				i++;
			}
			
			Integer num = pst.executeUpdate();
			return num;
		}catch(Exception e){
			e.printStackTrace();	
		}finally {
			JDBCTools.close(con, pst, null);
		}
		return null;
	}

	/**
	 * 更新ADMIN-ROLE关系条目
	 */
	@Override
	public Integer updateOneAdminRole(String sql, Object...objects) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCTools.getConnection();
			pst = con.prepareStatement(sql);
			int i = 1;
			for (Object object : objects) {
				
				pst.setObject(i, object);
				i++;
			}
			Integer num = pst.executeUpdate();
			return num;
		}catch(Exception e){
			e.printStackTrace();	
		}finally {
			JDBCTools.close(con, pst, null);
		}
		return null;
	}

}
